package com.qb.controller;

import com.qb.entity.DialogResult;
import com.qb.mapper.BtypeMapper;
import com.qb.pojo.Btype;
import com.qb.service.BtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("btype")
public class BtyteController {
    @Autowired
    private BtypeService btypeService;

    //大类别
    @RequestMapping("findAll")
    @ResponseBody
    public List<Btype> findAll() {
        List<Btype> all = btypeService.findAll();
        return all;
    }

    //小类别  复用 管理小类
    @RequestMapping("findSubTypeByTid")
    @ResponseBody
    public List<Btype> findSubTypeByTid(Integer typeid) {
        List<Btype> list = btypeService.findSubTypeByTid(typeid);
        return list;
    }

    //根据id进行唯一性查询： findById
    @RequestMapping("findById")
    @ResponseBody
    public Btype findById(Integer typeid) {
        Btype byId = btypeService.findById(typeid);
        return byId;
    }

    //修改大类数据 updateType
    @RequestMapping("updateType")
    @ResponseBody
    public Btype updateType(Btype btype) {
        DialogResult result = new DialogResult();
        boolean flag = btypeService.updateType(btype);

        if (flag == true) {
            result.setMsg("修改成功");
        } else {
            result.setError("修改失败");
        }
        return btype;
    }

    //删除大类  复用 删除小类
    @RequestMapping("deleteSubTypeBtn")
    @ResponseBody
    public DialogResult deleteSubTypeBtn(Integer typeid) throws Exception {
        DialogResult result = new DialogResult();
        boolean flag = btypeService.deleteSubTypeBtn(typeid);
        System.out.println(flag);
        if (flag == true) {
            result.setMsg("删除成功");
        } else {
            result.setError("删除失败");
        }
        return result;
    }

    //添加大类 saveType
    @RequestMapping("saveType")
    @ResponseBody
    public DialogResult saveType(Btype btype) {
        DialogResult result = new DialogResult();
        try {
            btypeService.save(btype);
            result.setMsg("保存成功");
        } catch (Exception e) {
            result.setMsg("保存失败");
            e.printStackTrace();
        }
        return result;
    }
}
